
#include "libc.h"
#include <crt.h>
#include <types.h>
#include <kfcntl.h>
#include "string.h"
#include "stdarg.h"
#include "dir.h"

void logo();
void command_exec (char * cmd);
static int shell_test (int argc, char *argv[]);
static int shell_cat (int argc, char *argv[]);
static int shell_uname (int argc, char *argv[]);
static int shell_exec (int argc ,char *argv[]);
static void shell_ls(int argc ,char *argv[]);


int main(int argc ,char *argv[])
{
     char buffer[256];
     char chr;
     int i ;
  
 logo();

 while (1)
    {
      memset (buffer, 0, sizeof(buffer));
      i = 0;
     
      printf ("Desiros-Shell-$ ");
         while(1)
	{
	  read (0, & chr, 1);
   
	  if (chr == '\n')
	    {
	      buffer[i] = '\0';
	      printf ("\n");
	      if (i != 0)
	     command_exec (buffer);
	      break;
	    }
	  else if (chr == '\b')
	    {
	      if (i > 0)
		{
		  i--;
		  printf ("\b");
		}
	    }

	  else if (i < 256)
	    {
	      buffer[i++] = chr;
	      printf ("%c", chr);
	    }
	  else
	    printf ("%c", chr);
	}
    }

	
                
	return 0;	

}


void logo(){
int i ;
for(i = 0 ;i < 50 ; i++)
    printf("\n");

printf("\t # # #    # # # #   #####  #  # # #    # # #   #####  \n");
printf("\t #     #  #        #       #  #    #  #     # #      \n");  
printf("\t #     #  # # #     #####  #  # # #   #     #  ####   \n");
printf("\t #     #  #              # #  #    #  #     #      # \n");
printf("\t # # #    # # # #   #####  #  #    #   # # #  #####  \n");
printf("\t ___________________________________________________ \n");
printf("\n\t\t\t By Everton.S.Baron\n\n");
for(i = 0 ;i < 7 ; i++)
    printf("\n");

}



void command_exec (char * cmd)
{
  char *c;
  int argc = 1, i;
  char **argv;

 
  for (c = cmd; *c != '\0'; c++)
    {
      if (*c == ' ')
	{
	  /* Skip all spaces */
	  while (*c && *c == ' ')
	    c++;

	  /* Reached the end of the command line ? */
	  if (! *c)
	    break;

	  argc++;
	}
    }

  argv = malloc ((argc+1) * sizeof(char*));

  argv[argc] = NULL; 
  if (argv == NULL){
    return;
     }


  for (i = 0, c = cmd; i < argc; i++)
    {
      argv[i] = c;
      while (*c != ' ' && *c != '\0')
	c++;
      *c = '\0';
      c++;
      /* Skip spaces */
      while (*c && *c == ' ')
	c++;
    }


   if (! strcmp (argv[0], "test"))
    {
      shell_test (argc, argv);
    } 
    else if (! strcmp (argv[0], "cat"))
    {
      shell_cat (argc, argv);
    }
    else if (! strcmp (argv[0], "uname")){
    shell_uname(argc, argv);
    }
    else if (! strcmp (argv[0], "exec"))
    {
      shell_exec (argc,argv);
    }  
    else if (! strcmp (argv[0], "ls"))
    {
	shell_ls (argc ,argv);
    }

    else
    printf ("Command not found\n");


  free (argv);
}


static int shell_test (int argc, char *argv[])
{
  int i;
  for (i = 0; i < argc; i++)
    {
      printf ("argv[%d] = %s\n", i, argv[i]);
    }
  return 0;
}


static int shell_cat (int argc, char *argv[])
{
  int fd;
  char buf[4096];
  char file[4096];
  int len;

  fd = open (argv[1], O_RDWR);
  if (fd < 0) {
    printf ("Cannot open '%s'\n", argv[1]);
    return -1;
  }

  while (1)
    {
      len = read (fd, buf, sizeof(buf)-1);
      if (len <= 0)
	break;
      buf[len] = '\0';

      printf ("%s", buf);
    }

  close (fd);
  return 0;
}


static int shell_uname (int argc, char *argv[])
{
  printf ("Desiros version 0.0.1 \n");
  return 0;
}


static int shell_exec (int argc,char *argv[])
{
    int statut;
    int pid  = 0;

  if (argc < 2)
    {
      printf("Usage: exec prog_name\n");
      return -1;
    }

   // char *file = argv[1];
               
  if((pid = fork()) == 0){
  execv("/core/boot/myprog2",NULL);
  exit(0);
 }     
    if( pid > 0){
     waitpid(pid, &statut) ;
    return  0;
    }
    
  
}

static void shell_ls(int argc ,char *argv[])
{
  struct directory_entry *dir;
  char buf[512], out[512],file[1024]; 
  int count;

   if (argc < 2)
    {
      printf("Usage: ls directory_name\n");
      return -1;
    }
  memset(file,0,sizeof(file));
  sprintf(file , "/core%s", argv[1]);
  printf(file);
  int fd_here = open(file, O_RDONLY | O_DIRECTORY);
  if (fd_here < 0)
    return;
   count = read(fd_here, buf, sizeof(buf));
   dir = (struct directory_entry*) buf;

			while(count>0 && dir->inode) {
				memcpy(out, &dir->name, dir->name_len);
				out[dir->name_len] = 0;
				
				printf("%s",out);
				printf("\n");

				count -= dir->rec_len;
				dir = (struct directory_entry*) ((char*) dir + dir->rec_len);
			}

  close(fd_here);
}
